# encoding: utf-8
import os

from datetime import datetime
from google.appengine.api import memcache
from google.appengine.ext import db

import models
import base_def

def update_setting_memcache():
	settings = models.Setting.all().fetch(limit=1000)
	if (settings is None) or (len(settings)==0):
		base_def.wgw_init()
		settings = models.Setting.all().fetch(limit=1000)
	memcache.set('settings',settings)

def update_page_memcache():
	pages = models.Article.all().filter('article_type','page').order('time_date').fetch(limit=1000)
	if (pages is None) or (len(pages)==0):
		base_def.init_page()
		pages = models.Article.all().filter('article_type','page').order('time_date').fetch(limit=1000)
	memcache.set('pages',pages)

def update_comment_memcache(count_comment):
	news_comments = models.Comment.all().order('-time_date').fetch(limit=count_comment)
	memcache.set('news_comments', news_comments)

def update_cundang_memcache():
	min_timedate = models.Article.all().filter('article_type','article').order('time_date').fetch(limit=1)
	if len(min_timedate)==1:
		min_timedate = min_timedate[0].time_date
		max_timedate = models.Article.all().order('-time_date').filter('article_type','article').fetch(limit=1)[0].time_date
		min_month = datetime.strftime(min_timedate,'%m')
		min_year = datetime.strftime(min_timedate,'%Y')
		max_month = datetime.strftime(max_timedate,'%m')
		max_year = datetime.strftime(max_timedate,'%Y')
		min_month_int = int(min_month)
		min_year_int = int(min_year)
		max_month_int = int(max_month)
		max_year_int = int(max_year)
		# 取得存档的日期的最小和最大值 END
		# 取得存档列表
		def wgw_add_z(q):
			if q < 10:
				q_g = '0'+str(q)
			else:
				q_g = str(q)
			return q_g
		cundang = []
		if (max_year_int == min_year_int):
			if (min_month_int == max_month_int):
				cundang.append(min_month+'_'+min_year+' 年')
			else:
				for month_q in range(min_month_int,max_month_int+1):
					cundang.append(wgw_add_z(month_q)+'_'+min_year+' 年')
		else:
			for year in range(min_year_int,max_year_int+1):
				if (year==min_year_int):
					for month_i_y in range(min_month_int,13):
						cundang.append(wgw_add_z(month_i_y)+'_'+str(year)+' 年')
				elif (min_year_int<year<max_year_int):
					for month_m_y in range(1,13):
						cundang.append(wgw_add_z(month_m_y)+'_'+str(year)+' 年')
				elif (year==max_year_int):
					for month_a_y in range(1,max_month_int+1):
						cundang.append(wgw_add_z(month_a_y)+'_'+str(year)+' 年')
	else:
		cundang = []
	# 取得存档列表 END
	memcache.set('cundang', cundang)

def update_category_memcache():
	categorys = models.Category.all().order('name').fetch(limit=1000)
	memcache.set('categorys', categorys)

def update_article_atom_memcache():
	articles = models.Article.all().order('-time_date').filter('article_type','article').fetch(limit=10)
	memcache.set('article_atom', articles)

def update_comment_atom_memcache():
	comments = models.Comment.all().order('-time_date').fetch(limit=10)
	memcache.set('comment_atom', comments)

def update_link_memcache():
	links = models.Link.all().order('name').fetch(limit=1000)
	memcache.set('links', links)

def get_setting_memcache():
	settings = memcache.get('settings')
	if (settings is None) or (len(settings)==0):
		update_setting_memcache()
		settings = get_setting_memcache()
	return settings

def get_page_memcache():
	pages = memcache.get('pages')
	if (pages is None) or (len(pages)==0):
		update_page_memcache()
		pages = get_page_memcache()
	return pages

def get_comment_memcache(count_comment):
	news_comments = memcache.get('news_comments')
	if news_comments is None:
		update_comment_memcache(count_comment)
		news_comments = get_comment_memcache()
	return news_comments

def get_cundang_memcache():
	cundang = memcache.get('cundang')
	if cundang is None:
		update_cundang_memcache()
		cundang = get_cundang_memcache()
	return cundang

def get_category_memcache():
	categorys = memcache.get('categorys')
	if categorys is None:
		update_category_memcache()
		categorys = get_category_memcache()
	return categorys

def get_article_atom_memcache():
	article_atom = memcache.get('article_atom')
	if article_atom is None:
		update_article_atom_memcache()
		article_atom = get_article_atom_memcache()
	return article_atom

def get_comment_atom_memcache():
	comment_atom = memcache.get('comment_atom')
	if comment_atom is None:
		update_comment_atom_memcache()
		comment_atom = get_comment_atom_memcache()
	return comment_atom

def get_link_memcache():
	links = memcache.get('links')
	if links is None:
		update_link_memcache()
		links = get_link_memcache()
	return links
